package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.User;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
//
//    @GetMapping("/")
//    public Result token(HttpSession session){
//
//    }

    //所有用户
    @GetMapping("user/list")
    public Result list(){

        List<User> list = userService.list();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        return new Result(200,"所有用户信息",true,map);
    }

    //获取单个用户信息
    @GetMapping("user/{id}")
    public Result get(@PathVariable("id") Integer id){

        if(null == id){
            return new Result(400,"id不能为空",false);
        }
        User user = userService.getById(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("user",user);
        return new Result(200,"用户信息",true,map);

    }

    @PutMapping("user")
    public Result update(@RequestBody User user){
        userService.update(user);
        return new Result(200,"修改用户信息成功",true);

    }

    @DeleteMapping("user/{id}")
    public Result delete(@PathVariable("id") Integer id){
        userService.delete(id);
        return new Result(200,"删除用户信息成功",true);
    }

    @PostMapping("user")
    public Result add(@RequestBody User user){
        userService.add(user);
        return new Result(200,"新增用户成功",true);

    }

}
